package com.yonyou.pmclouds.alterandvisa.process;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyou.pmclouds.alterandvisa.entity.AlterAndVisaVO;
import com.yonyou.pmclouds.alterandvisacategory.rmiitf.AlterAndVisaCategoryQuery;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogDetailVo;
import com.yonyou.pmclouds.projectlog.entity.ProjectLogJobVo;
import com.yonyou.pmclouds.projectlog.entity.VisaAlterationVo;
import com.yonyou.pmclouds.projectlog.rmiitf.ProjectLogMaintain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Map;

/**
 * 智能日志记录维护后置处理器，新建或更新都使用
 * 出错不要影响其他本来的流程
 * @author xiejianyu
 * 2019-09-22
 */
@Slf4j
@Component
public class AlterAndVisaProjectLogPostProcess implements SingleProcessor<AlterAndVisaVO> {

    private static ObjectMapper objectMapper = new ObjectMapper();
    @Autowired
    private ProjectLogMaintain projectLogMaintain;
    @Autowired
    private AlterAndVisaCategoryQuery alterAndVisaCategoryQuery;

    @Override
    public void process(AlterAndVisaVO vo, AlterAndVisaVO orig) throws BusinessException {
        //如果使用标准的 SingleInsert SingleUpdate SingleDelete 模板进行操作的话，新建时orig 为null，更新或删除时orig不为null
        //目前只有 pkAltervisacategory estimateMny 在更新的范围内
        try {
            ProjectLogJobVo jobVo = new ProjectLogJobVo();//最终放入队列的任务对象
            if (orig==null) {//新增记录
                VisaAlterationVo logDetailContent = new VisaAlterationVo();
                logDetailContent.setPkBill(vo.getPkAlterandvisa());
                logDetailContent.setBillType(vo.getBill_type());

                Map<String, String> categoryMap = alterAndVisaCategoryQuery.queryNameByIds(new String[]{vo.getPkAltervisacategory()});
                if (categoryMap!=null) {
                    logDetailContent.setAlterationName(categoryMap.get(vo.getPkAltervisacategory()));
                }
                logDetailContent.setAmount(vo.getEstimateMny());

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.VISA_ALTERATION);
                logDetail.setPkContentSource(vo.getPkAlterandvisa());
                logDetail.setContentJson(objectMapper.writeValueAsString(logDetailContent));
                logDetail.setCreator(vo.getCreator());
                logDetail.setCreationtime(vo.getCreationtime());
                logDetail.setModifier(vo.getCreator());
                logDetail.setModifytime(vo.getCreationtime());

                jobVo.setPkTenant(vo.getPkTenant());
                jobVo.setPkProject(vo.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(vo.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getCreationtime());
                jobVo.setUserId(vo.getCreator());
                jobVo.setAttachment(logDetail);
            } else {//更新记录
                VisaAlterationVo logDetailContent = new VisaAlterationVo();
                logDetailContent.setPkBill(orig.getPkAlterandvisa());
                logDetailContent.setBillType(orig.getBill_type());

                Map<String, String> categoryMap = alterAndVisaCategoryQuery.queryNameByIds(new String[]{vo.getPkAltervisacategory()});
                if (categoryMap!=null) {
                    logDetailContent.setAlterationName(categoryMap.get(vo.getPkAltervisacategory()));
                }
                logDetailContent.setAmount(vo.getEstimateMny());

                ProjectLogDetailVo logDetail = new ProjectLogDetailVo();
                logDetail.setDetailType(ProjectLogDetailVo.VISA_ALTERATION);
                logDetail.setPkContentSource(orig.getPkAlterandvisa());
                logDetail.setContentJson(objectMapper.writeValueAsString(logDetailContent));
                logDetail.setCreator(vo.getModifier());
                logDetail.setCreationtime(vo.getModifytime());
                logDetail.setModifier(vo.getModifier());
                logDetail.setModifytime(vo.getModifytime());

                jobVo.setPkTenant(orig.getPkTenant());
                jobVo.setPkProject(orig.getPk_project());

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(orig.getCreationtime());
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);

                jobVo.setLogDate(calendar.getTime());
                jobVo.setEventType(ProjectLogJobVo.EVENT_TYPE_DETAIL);
                jobVo.setEventTime(vo.getModifytime());
                jobVo.setUserId(vo.getModifier());
                jobVo.setAttachment(logDetail);
            }
            //pmCloudCacheManager.addObjToBlockingQueue(ProjectLogJobVo.JOB_QUEUE_NAME, jobVo);
            projectLogMaintain.upsert(jobVo);
        } catch (Exception e) {
            log.error("签证申请--智能日志处理出错，记录pk：{}", vo.getPkAlterandvisa());
            log.error("异常信息", e);
        }
    }
}